package ui.views;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.control.SearchTextComposite;
import ui.listener.ViewActivateEvent;
import ui.provider.ProductHierachyContent;
import ui.provider.ProductHierarchyLabel;
import ui.util.IPartId;

public class ProductHierarchyView extends ViewPart  
			implements	ITabbedPropertySheetPageContributor {
	
	public final static Logger logger = LoggerFactory.getLogger(ProductHierarchyView.class);
	private TreeViewer treeViewer;
	private SearchTextComposite swtComp;
	final Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
	
	public void createPartControl(Composite parent) {
		
		swtComp = new SearchTextComposite(parent, SWT.None);
//		Composite comp = new Composite(swtComp, SWT.None);
//		comp.setLayout(new GridLayout(1, true));
//		comp.setBackground(white);
		
		treeViewer = new TreeViewer(swtComp.getComp(), SWT.MULTI | SWT.H_SCROLL| SWT.V_SCROLL);
		treeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
//		treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL| SWT.V_SCROLL);
		treeViewer.setContentProvider(new ProductHierachyContent());
		treeViewer.setLabelProvider(new ProductHierarchyLabel());

		// Expand the tree
		treeViewer.setAutoExpandLevel(1);
		// Provide the input to the ContentProvider, every class except null
		treeViewer.setInput(treeViewer);

		// Selection Provider
		getSite().setSelectionProvider(treeViewer);
		
//		Add Event
		treeViewer.addPostSelectionChangedListener(new ViewActivateEvent(getSite().getPage(), IPartId.ProductHierarchy,IPartId.ProdCf));


		// createFiltersAndSorters();
		// createActions();
		// createMenus();
		// createToolbar();
		// createContextMenu();

	}

	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}

	// For Tabbed Property
//	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySheetPage.class){
			logger.debug("getAdapter in MarketVariableView:{}" );
			return new TabbedPropertySheetPage(this);
		}
//		logger.debug("getAdapter :{}" );
		return super.getAdapter(adapter);
	}
	@Override
	public String getContributorId() {
//		logger.debug("contributor Id:{}" , getSite().getId());
		return getSite().getId();
	}
	
}
